package com.mulanweb.api.module.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.model.ContentEvaluate;
import com.mulanweb.api.module.model.Order;
import com.mulanweb.api.module.model.User;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.Date;

/**
 * Created by zhf on 2016/8/24.
 */
@ValidateFile
public class ContentEvaluateController extends BaseController
{

    public void getEvaluates()
    {
        SQLGenerateHelper helper = new SQLGenerateHelper("content_evaluate", "create_time desc");
        helper.addCondition("type_id","in", getPara("typeId"));
        helper.addEqualsCondition("target_id", getParaToLong("targetId"));
        Page<ContentEvaluate> page = ContentEvaluate.dao.paginate(getPageNumber(), getPageSize(), helper.getSqlSelect(), helper.getSqlExceptSelect(), helper.getQueryParas());
        returnPageModelJson(page);
    }

    @ValidateForm
    public void save()
    {
        String openId = getSessionAttr("openId"); //"oULNBwOvt_D2A_8se1bQOQ06ky0w";//
        User   user   = User.dao.findFirst("SELECT * FROM `user` WHERE open_id = ?", openId);
        if (StrKit.notNull(user))
        {
            Integer typeId   = getParaToInt("typeId");
            Long    targetId = getParaToLong("targetId");
            String orderNumber = getPara("orderNumber");
            Order   order    = Order.dao.findFirst("SELECT * FROM `order` WHERE order_number=?",orderNumber);
            if (StrKit.notNull(order) && order.getStatusId() == 7)
            {
                ContentEvaluate contentEvaluate = ContentEvaluate.dao.findFirst("SELECT * FROM content_evaluate WHERE user_id= ? AND type_id=? AND target_id=? AND order_id = ?", user.getId(), typeId, targetId,order.getId());
                if (StrKit.notNull(contentEvaluate))
                {
                    returnJson(0, "已评论");
                } else
                {
                    Integer score   = getParaToInt("score");
                    String  content = getPara("content");
                    if (score > 10 || score < 0)
                    {
                        returnJson(0, "参数格式错误");
                    } else
                    {
                        contentEvaluate = new ContentEvaluate();
                        contentEvaluate.setTypeId(typeId);
                        contentEvaluate.setTargetId(targetId);
                        contentEvaluate.setScore(Float.parseFloat(score + ""));
                        contentEvaluate.setContent(content);
                        contentEvaluate.setUserId(user.getId());
                        contentEvaluate.setOrderId(order.getId());
                        contentEvaluate.setCreateTime(new Date());
                        if (contentEvaluate.save())
                        {
                            order.setStatusId(8L);//已评论状态
                            order.update();
                            returnSuccessJson();
                        } else
                        {
                            returnFailureJson();
                        }
                    }
                }
            }else{
                returnJson(0, "用户无权限发表评论");
            }
        } else
        {
            returnJson(0, "用户不合法");
        }
    }

    @ValidateForm
    public void getUserRemarks(){
        String openId = getSessionAttr("openId"); //"oULNBwOvt_D2A_8se1bQOQ06ky0w";//
        User   user   = User.dao.findFirst("SELECT * FROM `user` WHERE open_id = ?", openId);
        if (StrKit.notNull(user))
        {
            Integer typeId   = getParaToInt("typeId");
            Long    targetId = getParaToLong("targetId");
            String orderNumber = getPara("orderNumber");
            Order   order    = Order.dao.findFirst("SELECT * FROM `order` WHERE order_number=?",orderNumber);
            ContentEvaluate contentEvaluate = ContentEvaluate.dao.findFirst("SELECT * FROM content_evaluate WHERE user_id= ? AND type_id=? AND target_id=? AND order_id = ?", user.getId(), typeId, targetId,order.getId());
            if (StrKit.notNull(contentEvaluate))
            {
                returnSuccessJson(contentEvaluate);
            } else
            {
                returnJson(0,"未评论");
            }
        } else
        {
            returnJson(0, "用户不合法");
        }
    }

}
